/*      */ package com.zimbra.cs.account;
/*      */ 
/*      */ import com.zimbra.common.service.ServiceException;
/*      */ import com.zimbra.common.util.ByteUtil;
/*      */ import com.zimbra.common.util.CliUtil;
/*      */ import com.zimbra.common.util.DateUtil;
/*      */ import com.zimbra.common.util.Log;
/*      */ import com.zimbra.common.util.LogFactory;
/*      */ import com.zimbra.common.util.SetUtil;
/*      */ import com.zimbra.common.util.StringUtil;
/*      */ import com.zimbra.common.util.Version;
/*      */ import com.zimbra.common.util.ZimbraLog;
/*      */ import com.zimbra.cs.account.ldap.LdapProv;
/*      */ import java.io.BufferedWriter;
/*      */ import java.io.File;
/*      */ import java.io.FileOutputStream;
/*      */ import java.io.IOException;
/*      */ import java.io.OutputStream;
/*      */ import java.io.OutputStreamWriter;
/*      */ import java.io.PrintStream;
/*      */ import java.io.PrintWriter;
/*      */ import java.util.ArrayList;
/*      */ import java.util.Arrays;
/*      */ import java.util.Collection;
/*      */ import java.util.Collections;
/*      */ import java.util.Comparator;
/*      */ import java.util.HashMap;
/*      */ import java.util.HashSet;
/*      */ import java.util.Iterator;
/*      */ import java.util.LinkedList;
/*      */ import java.util.List;
/*      */ import java.util.Map;
/*      */ import java.util.Map.Entry;
/*      */ import java.util.Set;
/*      */ import org.apache.commons.cli.CommandLine;
/*      */ import org.apache.commons.cli.CommandLineParser;
/*      */ import org.apache.commons.cli.GnuParser;
/*      */ import org.apache.commons.cli.HelpFormatter;
/*      */ import org.apache.commons.cli.Option;
/*      */ import org.apache.commons.cli.Options;
/*      */ import org.apache.commons.cli.ParseException;
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ public class AttributeManagerUtil
/*      */ {
/*   62 */   private static Log logger = LogFactory.getLog(AttributeManagerUtil.class);
/*      */   
/*   64 */   private static Options options = new Options();
/*      */   
/*      */   private static final String ML_CONT_PREFIX = "  ";
/*      */   
/*      */   private final AttributeManager attrMgr;
/*      */   
/*      */   static
/*      */   {
/*   72 */     options.addOption("h", "help", false, "display this  usage info");
/*   73 */     options.addOption("o", "output", true, "output file (default it to generate output to stdout)");
/*   74 */     options.addOption("a", "action", true, "[generateLdapSchema | generateGlobalConfigLdif | generateDefaultCOSLdif | generateSchemaLdif]");
/*   75 */     options.addOption("t", "template", true, "template for LDAP schema");
/*   76 */     options.addOption("r", "regenerateFile", true, "Java file to regenerate");
/*      */     
/*      */ 
/*   79 */     Option iopt = new Option("i", "input", true, "attrs definition xml input file (can repeat)");
/*   80 */     iopt.setArgs(-2);
/*   81 */     options.addOption(iopt);
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*   86 */     Option copt = new Option("c", "inclass", true, "list attrs in class  (can repeat)");
/*   87 */     copt.setArgs(-2);
/*   88 */     options.addOption(copt);
/*      */     
/*   90 */     Option nopt = new Option("n", "notinclass", true, "not list attrs in class  (can repeat)");
/*   91 */     nopt.setArgs(-2);
/*   92 */     options.addOption(nopt);
/*      */     
/*   94 */     Option fopt = new Option("f", "flags", true, "flags to print  (can repeat)");
/*   95 */     fopt.setArgs(-2);
/*   96 */     options.addOption(fopt);
/*      */   }
/*      */   
/*      */   private static enum Action {
/*  100 */     dump, 
/*  101 */     generateDefaultCOSLdif, 
/*  102 */     generateDefaultExternalCOSLdif, 
/*  103 */     generateGetters, 
/*  104 */     generateGlobalConfigLdif, 
/*  105 */     generateLdapSchema, 
/*  106 */     generateMessageProperties, 
/*  107 */     generateProvisioning, 
/*  108 */     generateSchemaLdif, 
/*  109 */     listAttrs;
/*      */     
/*      */     private Action() {}
/*      */   }
/*      */   
/*      */   static class CLOptions {
/*  115 */     private static String get(String key, String defaultValue) { String value = System.getProperty(key);
/*  116 */       if (value == null) {
/*  117 */         return defaultValue;
/*      */       }
/*  119 */       return value;
/*      */     }
/*      */     
/*      */     public static String buildVersion() {
/*  123 */       return get("zimbra.version", "unknown");
/*      */     }
/*      */     
/*      */     public static String getBaseDn(String entry) {
/*  127 */       return get(entry + ".basedn", "cn=zimbra");
/*      */     }
/*      */     
/*      */     public static String getEntryName(String entry, String defaultValue) {
/*  131 */       return get(entry + ".name", defaultValue);
/*      */     }
/*      */     
/*      */     public static String getEntryId(String entry, String defaultValue) {
/*  135 */       return get(entry + ".id", defaultValue);
/*      */     }
/*      */   }
/*      */   
/*      */   private AttributeManagerUtil(AttributeManager am) {
/*  140 */     this.attrMgr = am;
/*      */   }
/*      */   
/*      */   private Map<String, AttributeInfo> getAttrs() {
/*  144 */     return this.attrMgr.getAttrs();
/*      */   }
/*      */   
/*      */   private Map<String, AttributeManager.ObjectClassInfo> getOCs() {
/*  148 */     return this.attrMgr.getOCs();
/*      */   }
/*      */   
/*      */   private Map<Integer, String> getGroupMap() {
/*  152 */     return this.attrMgr.getGroupMap();
/*      */   }
/*      */   
/*      */   private Map<Integer, String> getOCGroupMap() {
/*  156 */     return this.attrMgr.getOCGroupMap();
/*      */   }
/*      */   
/*      */   private String doNotModifyDisclaimer(String prefix) {
/*  160 */     return FileGenUtil.genDoNotModifyDisclaimer(prefix, AttributeManagerUtil.class.getSimpleName(), CLOptions.buildVersion());
/*      */   }
/*      */   
/*      */   private void generateDefaultCOSLdif(PrintWriter pw) {
/*  164 */     pw.println(doNotModifyDisclaimer("#"));
/*  165 */     pw.println("#");
/*  166 */     pw.println("# LDAP entry for the default Zimbra COS.");
/*  167 */     pw.println("#");
/*      */     
/*  169 */     String baseDn = CLOptions.getBaseDn("cos");
/*  170 */     String cosName = CLOptions.getEntryName("cos", "default");
/*  171 */     String cosId = CLOptions.getEntryId("cos", "e00428a1-0c00-11d9-836a-000d93afea2a");
/*      */     
/*  173 */     pw.println("dn: cn=" + cosName + ",cn=cos," + baseDn);
/*  174 */     pw.println("cn: " + cosName);
/*  175 */     pw.println("objectclass: zimbraCOS");
/*  176 */     pw.println("zimbraId: " + cosId);
/*  177 */     pw.println("description: The " + cosName + " COS");
/*      */     
/*  179 */     List<String> out = new LinkedList();
/*  180 */     for (Iterator i$ = getAttrs().values().iterator(); i$.hasNext();) { attr = (AttributeInfo)i$.next();
/*  181 */       List<String> gcv = attr.getDefaultCosValues();
/*  182 */       if (gcv != null) {
/*  183 */         for (String v : gcv)
/*  184 */           out.add(attr.getName() + ": " + v);
/*      */       }
/*      */     }
/*      */     AttributeInfo attr;
/*  188 */     String[] outs = (String[])out.toArray(new String[out.size()]);
/*  189 */     Arrays.sort(outs);
/*  190 */     for (String o : outs) {
/*  191 */       pw.println(o);
/*      */     }
/*      */   }
/*      */   
/*      */   private void generateDefaultExternalCOSLdif(PrintWriter pw) {
/*  196 */     pw.println(doNotModifyDisclaimer("#"));
/*  197 */     pw.println("#");
/*  198 */     pw.println("# LDAP entry for default COS for external user accounts.");
/*  199 */     pw.println("#");
/*      */     
/*  201 */     String baseDn = CLOptions.getBaseDn("cos");
/*  202 */     String cosName = CLOptions.getEntryName("cos", "defaultExternal");
/*  203 */     String cosId = CLOptions.getEntryId("cos", "f27456a8-0c00-11d9-280a-286d93afea2g");
/*      */     
/*  205 */     pw.println("dn: cn=" + cosName + ",cn=cos," + baseDn);
/*  206 */     pw.println("cn: " + cosName);
/*  207 */     pw.println("objectclass: zimbraCOS");
/*  208 */     pw.println("zimbraId: " + cosId);
/*  209 */     pw.println("description: The default external users COS");
/*      */     
/*  211 */     List<String> out = new LinkedList();
/*  212 */     for (Iterator i$ = getAttrs().values().iterator(); i$.hasNext();) { attr = (AttributeInfo)i$.next();
/*  213 */       List<String> defaultValues = attr.getDefaultExternalCosValues();
/*  214 */       if ((defaultValues != null) && (!defaultValues.isEmpty())) {
/*  215 */         for (String v : defaultValues) {
/*  216 */           out.add(attr.getName() + ": " + v);
/*      */         }
/*      */       } else {
/*  219 */         defaultValues = attr.getDefaultCosValues();
/*  220 */         if (defaultValues != null) {
/*  221 */           for (String v : defaultValues)
/*  222 */             out.add(attr.getName() + ": " + v);
/*      */         }
/*      */       }
/*      */     }
/*      */     AttributeInfo attr;
/*  227 */     String[] outs = (String[])out.toArray(new String[out.size()]);
/*  228 */     Arrays.sort(outs);
/*  229 */     for (String o : outs) {
/*  230 */       pw.println(o);
/*      */     }
/*      */   }
/*      */   
/*      */   private void generateGlobalConfigLdif(PrintWriter pw) {
/*  235 */     pw.println(doNotModifyDisclaimer("#"));
/*  236 */     pw.println("#");
/*  237 */     pw.println("# LDAP entry that contains initial default Zimbra global config.");
/*  238 */     pw.println("#");
/*      */     
/*  240 */     String baseDn = CLOptions.getBaseDn("config");
/*  241 */     pw.println("dn: cn=config," + baseDn);
/*  242 */     pw.println("objectclass: organizationalRole");
/*  243 */     pw.println("cn: config");
/*  244 */     pw.println("objectclass: zimbraGlobalConfig");
/*      */     
/*  246 */     List<String> out = new LinkedList();
/*  247 */     for (Iterator i$ = getAttrs().values().iterator(); i$.hasNext();) { attr = (AttributeInfo)i$.next();
/*  248 */       List<String> gcv = attr.getGlobalConfigValues();
/*  249 */       if (gcv != null) {
/*  250 */         for (String v : gcv)
/*  251 */           out.add(attr.getName() + ": " + v);
/*      */       }
/*      */     }
/*      */     AttributeInfo attr;
/*  255 */     String[] outs = (String[])out.toArray(new String[out.size()]);
/*  256 */     Arrays.sort(outs);
/*  257 */     for (String o : outs) {
/*  258 */       pw.println(o);
/*      */     }
/*      */   }
/*      */   
/*      */   private List<AttributeInfo> getAttrList(int groupId)
/*      */   {
/*  264 */     List<AttributeInfo> list = new ArrayList(getAttrs().size());
/*  265 */     for (AttributeInfo ai : getAttrs().values()) {
/*  266 */       if ((ai.getId() > -1) && (ai.getGroupId() == groupId)) {
/*  267 */         list.add(ai);
/*      */       }
/*      */     }
/*  270 */     return list;
/*      */   }
/*      */   
/*      */   private void sortAttrsByOID(List<AttributeInfo> list) {
/*  274 */     Collections.sort(list, new Comparator()
/*      */     {
/*      */       public int compare(AttributeInfo a1, AttributeInfo b1) {
/*  277 */         return a1.getId() - b1.getId();
/*      */       }
/*      */     });
/*      */   }
/*      */   
/*      */   private void sortAttrsByName(List<AttributeInfo> list) {
/*  283 */     Collections.sort(list, new Comparator()
/*      */     {
/*      */       public int compare(AttributeInfo a1, AttributeInfo b1) {
/*  286 */         return a1.getName().compareTo(b1.getName());
/*      */       }
/*      */     });
/*      */   }
/*      */   
/*      */   private List<AttributeManager.ObjectClassInfo> getOCList(int groupId) {
/*  292 */     List<AttributeManager.ObjectClassInfo> list = new ArrayList(getOCs().size());
/*  293 */     for (AttributeManager.ObjectClassInfo oci : getOCs().values()) {
/*  294 */       if ((oci.getId() > -1) && (oci.getGroupId() == groupId)) {
/*  295 */         list.add(oci);
/*      */       }
/*      */     }
/*  298 */     return list;
/*      */   }
/*      */   
/*      */   private void sortOCsByOID(List<AttributeManager.ObjectClassInfo> list) {
/*  302 */     Collections.sort(list, new Comparator()
/*      */     {
/*      */       public int compare(AttributeManager.ObjectClassInfo oc1, AttributeManager.ObjectClassInfo oc2) {
/*  305 */         return oc1.getId() - oc2.getId();
/*      */       }
/*      */     });
/*      */   }
/*      */   
/*      */   private void sortOCsByName(List<AttributeManager.ObjectClassInfo> list) {
/*  311 */     Collections.sort(list, new Comparator()
/*      */     {
/*      */       public int compare(AttributeManager.ObjectClassInfo oc1, AttributeManager.ObjectClassInfo oc2) {
/*  314 */         return oc1.getName().compareTo(oc2.getName());
/*      */       }
/*      */     });
/*      */   }
/*      */   
/*      */   private static String sortCSL(String in)
/*      */   {
/*  321 */     String[] ss = in.split("\\s*,\\s*");
/*  322 */     Arrays.sort(ss);
/*  323 */     StringBuilder sb = new StringBuilder(in.length());
/*  324 */     for (int i = 0; i < ss.length; i++) {
/*  325 */       sb.append(ss[i]);
/*  326 */       if (i < ss.length - 1) {
/*  327 */         sb.append(", ");
/*      */       }
/*      */     }
/*  330 */     return sb.toString();
/*      */   }
/*      */   
/*      */ 
/*      */   private String rfc4512Dstring(String unescaped)
/*      */   {
/*  336 */     String escaped = unescaped.replace("\\", "\\5C").replace("'", "\\27");
/*  337 */     return escaped;
/*      */   }
/*      */   
/*      */   private void buildSchemaBanner(StringBuilder BANNER)
/*      */   {
/*  342 */     BANNER.append(doNotModifyDisclaimer("#"));
/*  343 */     BANNER.append("#\n");
/*  344 */     BANNER.append("# Zimbra LDAP Schema\n");
/*  345 */     BANNER.append("#\n");
/*  346 */     BANNER.append("#\n");
/*  347 */     BANNER.append("#\n");
/*  348 */     BANNER.append("# our root OID (http://www.iana.org/assignments/enterprise-numbers)\n");
/*  349 */     BANNER.append("#\n");
/*  350 */     BANNER.append("#  1.3.6.1.4.1.19348\n");
/*  351 */     BANNER.append("#  1.3.6.1.4.1.19348.2      LDAP elements\n");
/*  352 */     BANNER.append("#  1.3.6.1.4.1.19348.2.1    Attribute Types\n");
/*  353 */     BANNER.append("#  1.3.6.1.4.1.19348.2.2    Object Classes\n");
/*  354 */     BANNER.append("#");
/*      */   }
/*      */   
/*      */   private void buildZimbraRootOIDs(StringBuilder ZIMBRA_ROOT_OIDS, String prefix) {
/*  358 */     ZIMBRA_ROOT_OIDS.append(prefix + "ZimbraRoot 1.3.6.1.4.1.19348\n");
/*  359 */     ZIMBRA_ROOT_OIDS.append(prefix + "ZimbraLDAP ZimbraRoot:2\n");
/*      */   }
/*      */   
/*      */ 
/*      */   private void buildAttrDef(StringBuilder ATTRIBUTE_DEFINITIONS, AttributeInfo ai)
/*      */   {
/*  365 */     String syntax = null;
/*  366 */     String substr = null;
/*  367 */     String equality = null;
/*  368 */     String ordering = null;
/*      */     String lengthSuffix;
/*  370 */     switch (ai.getType()) {
/*      */     case TYPE_BOOLEAN: 
/*  372 */       syntax = "1.3.6.1.4.1.1466.115.121.1.7";
/*  373 */       equality = "booleanMatch";
/*  374 */       break;
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */     case TYPE_BINARY: 
/*  403 */       lengthSuffix = "";
/*  404 */       if (ai.getMax() != Long.MAX_VALUE) {
/*  405 */         lengthSuffix = "{" + ai.getMax() + "}";
/*      */       }
/*  407 */       syntax = "1.3.6.1.4.1.1466.115.121.1.40" + lengthSuffix;
/*  408 */       equality = "octetStringMatch";
/*  409 */       break;
/*      */     
/*      */ 
/*      */ 
/*      */     case TYPE_CERTIFICATE: 
/*  414 */       lengthSuffix = "";
/*  415 */       if (ai.getMax() != Long.MAX_VALUE) {
/*  416 */         lengthSuffix = "{" + ai.getMax() + "}";
/*      */       }
/*  418 */       syntax = "1.3.6.1.4.1.1466.115.121.1.8" + lengthSuffix;
/*  419 */       equality = "certificateExactMatch";
/*  420 */       break;
/*      */     case TYPE_EMAIL: 
/*      */     case TYPE_EMAILP: 
/*      */     case TYPE_CS_EMAILP: 
/*  424 */       syntax = "1.3.6.1.4.1.1466.115.121.1.26{256}";
/*  425 */       equality = "caseIgnoreIA5Match";
/*  426 */       substr = "caseIgnoreSubstringsMatch";
/*  427 */       break;
/*      */     
/*      */     case TYPE_GENTIME: 
/*  430 */       syntax = "1.3.6.1.4.1.1466.115.121.1.24";
/*  431 */       equality = "generalizedTimeMatch";
/*  432 */       ordering = "generalizedTimeOrderingMatch ";
/*  433 */       break;
/*      */     
/*      */     case TYPE_ID: 
/*  436 */       syntax = "1.3.6.1.4.1.1466.115.121.1.15{256}";
/*  437 */       equality = "caseIgnoreMatch";
/*  438 */       substr = "caseIgnoreSubstringsMatch";
/*  439 */       break;
/*      */     
/*      */     case TYPE_DURATION: 
/*  442 */       syntax = "1.3.6.1.4.1.1466.115.121.1.26{32}";
/*  443 */       equality = "caseIgnoreIA5Match";
/*  444 */       break;
/*      */     
/*      */     case TYPE_ENUM: 
/*  447 */       int maxLen = Math.max(32, ai.getEnumValueMaxLength());
/*  448 */       syntax = "1.3.6.1.4.1.1466.115.121.1.15{" + maxLen + "}";
/*  449 */       equality = "caseIgnoreMatch";
/*  450 */       substr = "caseIgnoreSubstringsMatch";
/*  451 */       break;
/*      */     
/*      */     case TYPE_INTEGER: 
/*      */     case TYPE_PORT: 
/*      */     case TYPE_LONG: 
/*  456 */       syntax = "1.3.6.1.4.1.1466.115.121.1.27";
/*  457 */       equality = "integerMatch";
/*  458 */       break;
/*      */     
/*      */     case TYPE_STRING: 
/*      */     case TYPE_REGEX: 
/*  462 */       lengthSuffix = "";
/*  463 */       if (ai.getMax() != Long.MAX_VALUE) {
/*  464 */         lengthSuffix = "{" + ai.getMax() + "}";
/*      */       }
/*  466 */       syntax = "1.3.6.1.4.1.1466.115.121.1.15" + lengthSuffix;
/*  467 */       equality = "caseIgnoreMatch";
/*  468 */       substr = "caseIgnoreSubstringsMatch";
/*  469 */       break;
/*      */     
/*      */     case TYPE_ASTRING: 
/*  472 */       lengthSuffix = "";
/*  473 */       if (ai.getMax() != Long.MAX_VALUE) {
/*  474 */         lengthSuffix = "{" + ai.getMax() + "}";
/*      */       }
/*  476 */       syntax = "1.3.6.1.4.1.1466.115.121.1.26" + lengthSuffix;
/*  477 */       equality = "caseIgnoreIA5Match";
/*  478 */       substr = "caseIgnoreSubstringsMatch";
/*  479 */       break;
/*      */     
/*      */     case TYPE_OSTRING: 
/*  482 */       lengthSuffix = "";
/*  483 */       if (ai.getMax() != Long.MAX_VALUE) {
/*  484 */         lengthSuffix = "{" + ai.getMax() + "}";
/*      */       }
/*  486 */       syntax = "1.3.6.1.4.1.1466.115.121.1.40" + lengthSuffix;
/*  487 */       equality = "octetStringMatch";
/*  488 */       break;
/*      */     
/*      */     case TYPE_CSTRING: 
/*  491 */       lengthSuffix = "";
/*  492 */       if (ai.getMax() != Long.MAX_VALUE) {
/*  493 */         lengthSuffix = "{" + ai.getMax() + "}";
/*      */       }
/*  495 */       syntax = "1.3.6.1.4.1.1466.115.121.1.15" + lengthSuffix;
/*  496 */       equality = "caseExactMatch";
/*  497 */       substr = "caseExactSubstringsMatch";
/*  498 */       break;
/*      */     
/*      */     case TYPE_PHONE: 
/*  501 */       lengthSuffix = "";
/*  502 */       if (ai.getMax() != Long.MAX_VALUE) {
/*  503 */         lengthSuffix = "{" + ai.getMax() + "}";
/*      */       }
/*  505 */       syntax = "1.3.6.1.4.1.1466.115.121.1.50" + lengthSuffix;
/*  506 */       equality = "telephoneNumberMatch";
/*  507 */       substr = "telephoneNumberSubstringsMatch";
/*  508 */       break;
/*      */     
/*      */     default: 
/*  511 */       throw new RuntimeException("unknown type encountered!");
/*      */     }
/*      */     
/*  514 */     ATTRIBUTE_DEFINITIONS.append("( " + ai.getName() + "\n");
/*  515 */     ATTRIBUTE_DEFINITIONS.append("  NAME ( '" + ai.getName() + "' )\n");
/*  516 */     ATTRIBUTE_DEFINITIONS.append("  DESC '" + rfc4512Dstring(ai.getDescription()) + "'\n");
/*      */     
/*  518 */     ATTRIBUTE_DEFINITIONS.append("  SYNTAX " + syntax);
/*      */     
/*  520 */     if (equality != null) {
/*  521 */       ATTRIBUTE_DEFINITIONS.append("\n  EQUALITY " + equality);
/*      */     }
/*      */     
/*  524 */     if (substr != null) {
/*  525 */       ATTRIBUTE_DEFINITIONS.append("\n  SUBSTR " + substr);
/*      */     }
/*      */     
/*  528 */     if (ordering != null) {
/*  529 */       ATTRIBUTE_DEFINITIONS.append("\n  ORDERING " + ordering);
/*  530 */     } else if (ai.getOrder() != null) {
/*  531 */       ATTRIBUTE_DEFINITIONS.append("\n  ORDERING " + ai.getOrder());
/*      */     }
/*      */     
/*  534 */     if (ai.getCardinality() == AttributeCardinality.single) {
/*  535 */       ATTRIBUTE_DEFINITIONS.append("\n  SINGLE-VALUE");
/*      */     }
/*      */     
/*  538 */     ATTRIBUTE_DEFINITIONS.append(")");
/*      */   }
/*      */   
/*      */   private void buildObjectClassOIDs(StringBuilder OC_GROUP_OIDS, StringBuilder OC_OIDS, String prefix)
/*      */   {
/*  543 */     for (Iterator<Integer> iter = getOCGroupMap().keySet().iterator(); iter.hasNext();) {
/*  544 */       i = ((Integer)iter.next()).intValue();
/*      */       
/*      */ 
/*  547 */       OC_GROUP_OIDS.append(prefix + (String)getOCGroupMap().get(Integer.valueOf(i)) + " ZimbraLDAP:" + i + "\n");
/*      */       
/*      */ 
/*  550 */       List<AttributeManager.ObjectClassInfo> list = getOCList(i);
/*      */       
/*      */ 
/*  553 */       sortOCsByOID(list);
/*      */       
/*  555 */       for (AttributeManager.ObjectClassInfo oci : list) {
/*  556 */         OC_OIDS.append(prefix + oci.getName() + " " + (String)getOCGroupMap().get(Integer.valueOf(i)) + ':' + oci.getId() + "\n");
/*      */       }
/*      */     }
/*      */     
/*      */ 
/*      */     int i;
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */   private void buildObjectClassDefs(StringBuilder OC_DEFINITIONS, String prefix, boolean blankLineSeperator)
/*      */   {
/*  568 */     for (AttributeClass cls : )
/*      */     {
/*  570 */       String ocName = cls.getOCName();
/*  571 */       String ocCanonicalName = ocName.toLowerCase();
/*  572 */       AttributeManager.ObjectClassInfo oci = (AttributeManager.ObjectClassInfo)getOCs().get(ocCanonicalName);
/*  573 */       if (oci != null)
/*      */       {
/*      */ 
/*      */ 
/*  577 */         List<String> comment = oci.getComment();
/*  578 */         OC_DEFINITIONS.append("#\n");
/*  579 */         for (String line : comment) {
/*  580 */           if (line.length() > 0) {
/*  581 */             OC_DEFINITIONS.append("# " + line + "\n");
/*      */           } else
/*  583 */             OC_DEFINITIONS.append("#\n");
/*      */         }
/*  585 */         OC_DEFINITIONS.append("#\n");
/*      */         
/*  587 */         OC_DEFINITIONS.append(prefix + "( " + oci.getName() + "\n");
/*  588 */         OC_DEFINITIONS.append("  NAME '" + oci.getName() + "'\n");
/*  589 */         OC_DEFINITIONS.append("  DESC '" + rfc4512Dstring(oci.getDescription()) + "'\n");
/*  590 */         OC_DEFINITIONS.append("  SUP ");
/*  591 */         for (String sup : oci.getSuperOCs())
/*  592 */           OC_DEFINITIONS.append(sup);
/*  593 */         OC_DEFINITIONS.append(" " + oci.getType() + "\n");
/*      */         
/*  595 */         StringBuilder value = new StringBuilder();
/*  596 */         buildObjectClassAttrs(cls, value);
/*      */         
/*  598 */         OC_DEFINITIONS.append(value);
/*  599 */         OC_DEFINITIONS.append(")\n");
/*      */         
/*  601 */         if (blankLineSeperator)
/*  602 */           OC_DEFINITIONS.append("\n");
/*      */       }
/*      */     }
/*      */   }
/*      */   
/*      */   private void buildObjectClassAttrs(AttributeClass cls, StringBuilder value) {
/*  608 */     List<String> must = new LinkedList();
/*  609 */     List<String> may = new LinkedList();
/*  610 */     for (AttributeInfo ai : getAttrs().values()) {
/*  611 */       if (ai.requiredInClass(cls)) {
/*  612 */         must.add(ai.getName());
/*      */       }
/*  614 */       if (ai.optionalInClass(cls)) {
/*  615 */         may.add(ai.getName());
/*      */       }
/*      */     }
/*  618 */     Collections.sort(must);
/*  619 */     Collections.sort(may);
/*      */     
/*  621 */     if (!must.isEmpty()) {
/*  622 */       value.append("  MUST (\n");
/*  623 */       Iterator<String> mustIter = must.iterator();
/*      */       for (;;) {
/*  625 */         value.append("    ").append((String)mustIter.next());
/*  626 */         if (!mustIter.hasNext()) {
/*      */           break;
/*      */         }
/*  629 */         value.append(" $\n");
/*      */       }
/*  631 */       value.append("\n  )\n");
/*      */     }
/*  633 */     if (!may.isEmpty()) {
/*  634 */       value.append("  MAY (\n");
/*  635 */       Iterator<String> mayIter = may.iterator();
/*      */       for (;;) {
/*  637 */         value.append("    ").append((String)mayIter.next());
/*  638 */         if (!mayIter.hasNext()) {
/*      */           break;
/*      */         }
/*  641 */         value.append(" $\n");
/*      */       }
/*  643 */       value.append("\n  )\n");
/*      */     }
/*  645 */     value.append("  ");
/*      */   }
/*      */   
/*      */ 
/*      */   private void generateLdapSchema(PrintWriter pw, String schemaTemplateFile)
/*      */     throws IOException
/*      */   {
/*  652 */     byte[] templateBytes = ByteUtil.getContent(new File(schemaTemplateFile));
/*  653 */     String templateString = new String(templateBytes, "utf-8");
/*      */     
/*  655 */     StringBuilder BANNER = new StringBuilder();
/*  656 */     StringBuilder ZIMBRA_ROOT_OIDS = new StringBuilder();
/*  657 */     StringBuilder GROUP_OIDS = new StringBuilder();
/*  658 */     StringBuilder ATTRIBUTE_OIDS = new StringBuilder();
/*  659 */     StringBuilder ATTRIBUTE_DEFINITIONS = new StringBuilder();
/*  660 */     StringBuilder OC_GROUP_OIDS = new StringBuilder();
/*  661 */     StringBuilder OC_OIDS = new StringBuilder();
/*  662 */     StringBuilder OC_DEFINITIONS = new StringBuilder();
/*      */     
/*  664 */     buildSchemaBanner(BANNER);
/*  665 */     buildZimbraRootOIDs(ZIMBRA_ROOT_OIDS, "objectIdentifier ");
/*      */     
/*  667 */     for (Iterator<Integer> iter = getGroupMap().keySet().iterator(); iter.hasNext();) {
/*  668 */       int i = ((Integer)iter.next()).intValue();
/*      */       
/*      */ 
/*  671 */       GROUP_OIDS.append("objectIdentifier " + (String)getGroupMap().get(Integer.valueOf(i)) + " ZimbraLDAP:" + i + "\n");
/*      */       
/*      */ 
/*  674 */       List<AttributeInfo> list = getAttrList(i);
/*      */       
/*      */ 
/*  677 */       sortAttrsByOID(list);
/*      */       
/*  679 */       for (AttributeInfo ai : list) {
/*  680 */         String parentOid = ai.getParentOid();
/*  681 */         if (parentOid == null) {
/*  682 */           ATTRIBUTE_OIDS.append("objectIdentifier " + ai.getName() + " " + (String)getGroupMap().get(Integer.valueOf(i)) + ':' + ai.getId() + "\n");
/*      */         } else {
/*  684 */           ATTRIBUTE_OIDS.append("objectIdentifier " + ai.getName() + " " + parentOid + "." + ai.getId() + "\n");
/*      */         }
/*      */       }
/*      */       
/*      */ 
/*  689 */       sortAttrsByName(list);
/*      */       
/*  691 */       for (AttributeInfo ai : list) {
/*  692 */         ATTRIBUTE_DEFINITIONS.append("attributetype ");
/*  693 */         buildAttrDef(ATTRIBUTE_DEFINITIONS, ai);
/*  694 */         ATTRIBUTE_DEFINITIONS.append("\n\n");
/*      */       }
/*      */     }
/*      */     
/*      */ 
/*  699 */     buildObjectClassOIDs(OC_GROUP_OIDS, OC_OIDS, "objectIdentifier ");
/*      */     
/*      */ 
/*  702 */     buildObjectClassDefs(OC_DEFINITIONS, "objectclass ", true);
/*      */     
/*  704 */     Map<String, String> templateFillers = new HashMap();
/*  705 */     templateFillers.put("BANNER", BANNER.toString());
/*  706 */     templateFillers.put("ZIMBRA_ROOT_OIDS", ZIMBRA_ROOT_OIDS.toString());
/*  707 */     templateFillers.put("GROUP_OIDS", GROUP_OIDS.toString());
/*  708 */     templateFillers.put("ATTRIBUTE_OIDS", ATTRIBUTE_OIDS.toString());
/*  709 */     templateFillers.put("OC_GROUP_OIDS", OC_GROUP_OIDS.toString());
/*  710 */     templateFillers.put("OC_OIDS", OC_OIDS.toString());
/*  711 */     templateFillers.put("ATTRIBUTE_DEFINITIONS", ATTRIBUTE_DEFINITIONS.toString());
/*  712 */     templateFillers.put("OC_DEFINITIONS", OC_DEFINITIONS.toString());
/*      */     
/*  714 */     pw.print(StringUtil.fillTemplate(templateString, templateFillers));
/*      */   }
/*      */   
/*      */   private void generateSchemaLdif(PrintWriter pw)
/*      */   {
/*  719 */     StringBuilder BANNER = new StringBuilder();
/*  720 */     StringBuilder ZIMBRA_ROOT_OIDS = new StringBuilder();
/*  721 */     StringBuilder ATTRIBUTE_GROUP_OIDS = new StringBuilder();
/*  722 */     StringBuilder ATTRIBUTE_OIDS = new StringBuilder();
/*  723 */     StringBuilder ATTRIBUTE_DEFINITIONS = new StringBuilder();
/*  724 */     StringBuilder OC_GROUP_OIDS = new StringBuilder();
/*  725 */     StringBuilder OC_OIDS = new StringBuilder();
/*  726 */     StringBuilder OC_DEFINITIONS = new StringBuilder();
/*      */     
/*  728 */     buildSchemaBanner(BANNER);
/*  729 */     buildZimbraRootOIDs(ZIMBRA_ROOT_OIDS, "olcObjectIdentifier: ");
/*      */     
/*  731 */     for (Iterator<Integer> iter = getGroupMap().keySet().iterator(); iter.hasNext();) {
/*  732 */       int i = ((Integer)iter.next()).intValue();
/*      */       
/*      */ 
/*  735 */       ATTRIBUTE_GROUP_OIDS.append("olcObjectIdentifier: " + (String)getGroupMap().get(Integer.valueOf(i)) + " ZimbraLDAP:" + i + "\n");
/*      */       
/*      */ 
/*  738 */       List<AttributeInfo> list = getAttrList(i);
/*      */       
/*      */ 
/*  741 */       sortAttrsByOID(list);
/*      */       
/*  743 */       for (AttributeInfo ai : list) {
/*  744 */         String parentOid = ai.getParentOid();
/*  745 */         if (parentOid == null) {
/*  746 */           ATTRIBUTE_OIDS.append("olcObjectIdentifier: " + ai.getName() + " " + (String)getGroupMap().get(Integer.valueOf(i)) + ':' + ai.getId() + "\n");
/*      */         } else {
/*  748 */           ATTRIBUTE_OIDS.append("olcObjectIdentifier: " + ai.getName() + " " + parentOid + "." + ai.getId() + "\n");
/*      */         }
/*      */       }
/*      */       
/*      */ 
/*  753 */       sortAttrsByName(list);
/*      */       
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*  769 */       ATTRIBUTE_DEFINITIONS.append("olcAttributeTypes: ( 1.2.840.113556.1.2.146\n");
/*  770 */       ATTRIBUTE_DEFINITIONS.append("  NAME ( 'company' )\n");
/*  771 */       ATTRIBUTE_DEFINITIONS.append("  SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{512}\n");
/*  772 */       ATTRIBUTE_DEFINITIONS.append("  EQUALITY caseIgnoreMatch\n");
/*  773 */       ATTRIBUTE_DEFINITIONS.append("  SUBSTR caseIgnoreSubstringsMatch\n");
/*  774 */       ATTRIBUTE_DEFINITIONS.append("  SINGLE-VALUE )\n");
/*      */       
/*  776 */       for (AttributeInfo ai : list) {
/*  777 */         ATTRIBUTE_DEFINITIONS.append("olcAttributeTypes: ");
/*  778 */         buildAttrDef(ATTRIBUTE_DEFINITIONS, ai);
/*  779 */         ATTRIBUTE_DEFINITIONS.append("\n");
/*      */       }
/*      */     }
/*      */     
/*      */ 
/*  784 */     buildObjectClassOIDs(OC_GROUP_OIDS, OC_OIDS, "olcObjectIdentifier: ");
/*      */     
/*      */ 
/*  787 */     buildObjectClassDefs(OC_DEFINITIONS, "olcObjectClasses: ", false);
/*      */     
/*  789 */     pw.println(BANNER);
/*  790 */     pw.println("dn: cn=zimbra,cn=schema,cn=config");
/*  791 */     pw.println("objectClass: olcSchemaConfig");
/*  792 */     pw.println("cn: zimbra");
/*  793 */     pw.print(ZIMBRA_ROOT_OIDS);
/*  794 */     pw.print(ATTRIBUTE_GROUP_OIDS);
/*  795 */     pw.print(ATTRIBUTE_OIDS);
/*  796 */     pw.print(OC_GROUP_OIDS);
/*  797 */     pw.print(OC_OIDS);
/*  798 */     pw.print(ATTRIBUTE_DEFINITIONS);
/*  799 */     pw.print(OC_DEFINITIONS);
/*      */   }
/*      */   
/*      */   private void generateMessageProperties(String outFile) throws IOException {
/*  803 */     StringBuilder result = new StringBuilder();
/*      */     
/*  805 */     result.append(doNotModifyDisclaimer("#"));
/*  806 */     result.append("# Zimbra LDAP attributes.\n");
/*  807 */     result.append("# \n");
/*      */     
/*  809 */     List<String> attrs = new ArrayList(getAttrs().keySet());
/*  810 */     Collections.sort(attrs);
/*      */     
/*  812 */     for (String attr : attrs) {
/*  813 */       AttributeInfo ai = (AttributeInfo)getAttrs().get(attr);
/*  814 */       String desc = ai.getDescription();
/*  815 */       if (desc != null) {
/*  816 */         String text = FileGenUtil.wrapComments(desc, 80, "  ", " \\").substring(2);
/*  817 */         result.append(ai.getName() + " = " + text + "\n");
/*      */       }
/*      */     }
/*      */     
/*  821 */     FileGenUtil.replaceFile(outFile, result.toString());
/*      */   }
/*      */   
/*      */   private void listAttrs(PrintWriter pw, String[] inClass, String[] notInClass, String[] printFlags) throws ServiceException {
/*  825 */     if (inClass == null) {
/*  826 */       usage("no class specified");
/*      */     }
/*  828 */     Set<String> attrsInClass = new HashSet();
/*  829 */     for (String c : inClass) {
/*  830 */       AttributeClass ac = AttributeClass.valueOf(c);
/*  831 */       SetUtil.union(attrsInClass, this.attrMgr.getAttrsInClass(ac));
/*      */     }
/*      */     
/*  834 */     Set<String> attrsNotInClass = new HashSet();
/*  835 */     if (notInClass != null) {
/*  836 */       for (String c : notInClass) {
/*  837 */         AttributeClass ac = AttributeClass.valueOf(c);
/*  838 */         SetUtil.union(attrsNotInClass, this.attrMgr.getAttrsInClass(ac));
/*      */       }
/*      */     }
/*      */     
/*  842 */     attrsInClass = SetUtil.subtract(attrsInClass, attrsNotInClass);
/*      */     
/*  844 */     List<String> list = new ArrayList(attrsInClass);
/*  845 */     Collections.sort(list);
/*      */     
/*  847 */     for (String a : list) {
/*  848 */       StringBuffer flags = new StringBuffer();
/*  849 */       if (printFlags != null) {
/*  850 */         for (String f : printFlags) {
/*  851 */           AttributeFlag af = AttributeFlag.valueOf(f);
/*  852 */           if (this.attrMgr.hasFlag(af, a)) {
/*  853 */             if (flags.length() > 0)
/*  854 */               flags.append(", ");
/*  855 */             flags.append(af.name());
/*      */           }
/*      */         }
/*      */         
/*  859 */         if (flags.length() > 0) {
/*  860 */           flags.insert(0, "(").append(")");
/*      */         }
/*      */       }
/*  863 */       System.out.println(a + " " + flags);
/*      */     }
/*      */   }
/*      */   
/*      */   private static String enumName(AttributeInfo ai)
/*      */   {
/*  869 */     String enumName = ai.getName();
/*  870 */     if (enumName.startsWith("zimbra")) enumName = enumName.substring(6);
/*  871 */     enumName = StringUtil.escapeJavaIdentifier(enumName.substring(0, 1).toUpperCase() + enumName.substring(1));
/*  872 */     return enumName;
/*      */   }
/*      */   
/*      */   private static void generateEnum(StringBuilder result, AttributeInfo ai) throws ServiceException
/*      */   {
/*  877 */     Map<String, String> values = new HashMap();
/*  878 */     for (String v : ai.getEnumSet()) {
/*  879 */       values.put(v, StringUtil.escapeJavaIdentifier(v));
/*      */     }
/*      */     
/*  882 */     String enumName = enumName(ai);
/*      */     
/*  884 */     result.append(String.format("%n", new Object[0]));
/*  885 */     result.append(String.format("    public static enum %s {%n", new Object[] { enumName }));
/*  886 */     Set<Map.Entry<String, String>> set = values.entrySet();
/*  887 */     int i = 1;
/*  888 */     for (Map.Entry<String, String> entry : set) {
/*  889 */       result.append(String.format("        %s(\"%s\")%s%n", new Object[] { entry.getValue(), entry.getKey(), i == set.size() ? ";" : "," }));
/*  890 */       i++;
/*      */     }
/*      */     
/*  893 */     result.append(String.format("        private String mValue;%n", new Object[0]));
/*  894 */     result.append(String.format("        private %s(String value) { mValue = value; }%n", new Object[] { enumName }));
/*  895 */     result.append(String.format("        public String toString() { return mValue; }%n", new Object[0]));
/*  896 */     result.append(String.format("        public static %s fromString(String s) throws ServiceException {%n", new Object[] { enumName }));
/*  897 */     result.append(String.format("            for (%s value : values()) {%n", new Object[] { enumName }));
/*  898 */     result.append(String.format("                if (value.mValue.equals(s)) return value;%n", new Object[0]));
/*  899 */     result.append(String.format("             }%n", new Object[0]));
/*  900 */     result.append(String.format("             throw ServiceException.INVALID_REQUEST(\"invalid value: \"+s+\", valid values: \"+ Arrays.asList(values()), null);%n", new Object[0]));
/*  901 */     result.append(String.format("        }%n", new Object[0]));
/*  902 */     for (Map.Entry<String, String> entry : set) {
/*  903 */       result.append(String.format("        public boolean is%s() { return this == %s;}%n", new Object[] { StringUtil.capitalize((String)entry.getValue()), entry.getValue() }));
/*      */     }
/*  905 */     result.append(String.format("    }%n", new Object[0]));
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   private void generateGetters(String inClass, String javaFile)
/*      */     throws ServiceException, IOException
/*      */   {
/*  916 */     if (inClass == null) {
/*  917 */       usage("no class specified");
/*      */     }
/*      */     
/*  920 */     AttributeClass ac = AttributeClass.valueOf(inClass);
/*  921 */     Set<String> attrsInClass = this.attrMgr.getAttrsInClass(ac);
/*      */     
/*      */ 
/*  924 */     if (ac == AttributeClass.account) {
/*  925 */       SetUtil.union(attrsInClass, this.attrMgr.getAttrsInClass(AttributeClass.mailRecipient));
/*      */     }
/*      */     
/*  928 */     List<String> list = new ArrayList(attrsInClass);
/*  929 */     Collections.sort(list);
/*      */     
/*  931 */     StringBuilder result = new StringBuilder();
/*      */     
/*  933 */     for (String a : list) {
/*  934 */       AttributeInfo ai = (AttributeInfo)getAttrs().get(a.toLowerCase());
/*  935 */       if (ai != null)
/*      */       {
/*      */ 
/*  938 */         switch (ai.getType()) {
/*      */         case TYPE_BINARY: 
/*      */         case TYPE_CERTIFICATE: 
/*      */         case TYPE_GENTIME: 
/*      */         case TYPE_DURATION: 
/*      */         case TYPE_ENUM: 
/*      */         case TYPE_PORT: 
/*  945 */           if (ai.getCardinality() != AttributeCardinality.multi) {
/*  946 */             generateGetter(result, ai, false, ac);
/*      */           }
/*  948 */           generateGetter(result, ai, true, ac);
/*  949 */           generateSetters(result, ai, false, SetterType.set);
/*  950 */           if ((ai.getType() == AttributeType.TYPE_GENTIME) || (ai.getType() == AttributeType.TYPE_ENUM) || (ai.getType() == AttributeType.TYPE_PORT))
/*      */           {
/*      */ 
/*  953 */             generateSetters(result, ai, true, SetterType.set);
/*      */           }
/*  955 */           generateSetters(result, ai, false, SetterType.unset);
/*  956 */           break;
/*      */         case TYPE_EMAIL: case TYPE_EMAILP: case TYPE_CS_EMAILP: case TYPE_ID: case TYPE_INTEGER: default: 
/*  958 */           if (ai.getName().equalsIgnoreCase("zimbraLocale")) {
/*  959 */             generateGetter(result, ai, true, ac);
/*      */           } else {
/*  961 */             generateGetter(result, ai, false, ac);
/*      */           }
/*  963 */           generateSetters(result, ai, false, SetterType.set);
/*  964 */           if (ai.getCardinality() == AttributeCardinality.multi) {
/*  965 */             generateSetters(result, ai, false, SetterType.add);
/*  966 */             generateSetters(result, ai, false, SetterType.remove);
/*      */           }
/*  968 */           generateSetters(result, ai, false, SetterType.unset);
/*      */         }
/*      */       }
/*      */     }
/*  972 */     FileGenUtil.replaceJavaFile(javaFile, result.toString());
/*      */   }
/*      */   
/*      */   private static String defaultValue(AttributeInfo ai, AttributeClass ac) { List<String> values;
/*      */     List<String> values;
/*  977 */     switch (ac) {
/*      */     case account: 
/*      */     case calendarResource: 
/*      */     case cos: 
/*  981 */       values = ai.getDefaultCosValues();
/*  982 */       break;
/*      */     case domain: 
/*  984 */       if (ai.hasFlag(AttributeFlag.domainInherited)) {
/*  985 */         values = ai.getGlobalConfigValues();
/*      */       } else
/*  987 */         return null;
/*      */       break;
/*      */     case server:  List<String> values;
/*  990 */       if (ai.hasFlag(AttributeFlag.serverInherited)) {
/*  991 */         values = ai.getGlobalConfigValues();
/*      */       } else
/*  993 */         return null;
/*      */       break;
/*      */     case globalConfig: 
/*  996 */       values = ai.getGlobalConfigValues();
/*  997 */       break;
/*      */     default: 
/*  999 */       return null;
/*      */     }
/* 1001 */     if ((values == null) || (values.size() == 0)) {
/* 1002 */       return null;
/*      */     }
/* 1004 */     if (ai.getCardinality() != AttributeCardinality.multi) {
/* 1005 */       return (String)values.get(0);
/*      */     }
/* 1007 */     StringBuilder result = new StringBuilder();
/* 1008 */     result.append("new String[] {");
/* 1009 */     boolean first = true;
/* 1010 */     for (String v : values) {
/* 1011 */       if (!first) result.append(","); else first = false;
/* 1012 */       result.append("\"");
/* 1013 */       result.append(v.replace("\"", "\\\""));
/* 1014 */       result.append("\"");
/*      */     }
/* 1016 */     result.append("}");
/* 1017 */     return result.toString();
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */   private static void generateGetter(StringBuilder result, AttributeInfo ai, boolean asString, AttributeClass ac)
/*      */     throws ServiceException
/*      */   {
/* 1025 */     String name = ai.getName();
/* 1026 */     AttributeType type = asString ? AttributeType.TYPE_STRING : ai.getType();
/* 1027 */     boolean asStringDoc = false;
/*      */     
/* 1029 */     String methodName = ai.getName();
/* 1030 */     if (methodName.startsWith("zimbra")) methodName = methodName.substring(6);
/* 1031 */     methodName = (type == AttributeType.TYPE_BOOLEAN ? "is" : "get") + methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
/* 1032 */     if (asString) { methodName = methodName + "AsString";
/*      */     }
/* 1034 */     String defaultValue = defaultValue(ai, ac);
/*      */     String javaType;
/* 1036 */     String javaBody; String javaDocReturns; String javaType; String javaBody; String javaDocReturns; switch (type) {
/*      */     case TYPE_BOOLEAN: 
/* 1038 */       defaultValue = "TRUE".equalsIgnoreCase(defaultValue) ? "true" : "false";
/* 1039 */       javaType = "boolean";
/* 1040 */       javaBody = String.format("return getBooleanAttr(Provisioning.A_%s, %s);", new Object[] { name, defaultValue });
/* 1041 */       javaDocReturns = String.format(", or %s if unset", new Object[] { defaultValue });
/* 1042 */       break;
/*      */     case TYPE_BINARY: 
/*      */     case TYPE_CERTIFICATE: 
/* 1045 */       defaultValue = "null";
/* 1046 */       javaType = "byte[]";
/* 1047 */       javaBody = String.format("return getBinaryAttr(Provisioning.A_%s);", new Object[] { name });
/* 1048 */       javaDocReturns = String.format(", or null if unset", new Object[] { defaultValue });
/* 1049 */       break;
/*      */     case TYPE_INTEGER: 
/* 1051 */       if (defaultValue == null) defaultValue = "-1";
/* 1052 */       javaType = "int";
/* 1053 */       javaBody = String.format("return getIntAttr(Provisioning.A_%s, %s);", new Object[] { name, defaultValue });
/* 1054 */       javaDocReturns = String.format(", or %s if unset", new Object[] { defaultValue });
/* 1055 */       break;
/*      */     case TYPE_PORT: 
/* 1057 */       if (defaultValue == null) defaultValue = "-1";
/* 1058 */       javaType = "int";
/* 1059 */       javaBody = String.format("return getIntAttr(Provisioning.A_%s, %s);", new Object[] { name, defaultValue });
/* 1060 */       javaDocReturns = String.format(", or %s if unset", new Object[] { defaultValue });
/* 1061 */       asStringDoc = true;
/* 1062 */       break;
/*      */     case TYPE_ENUM: 
/* 1064 */       javaType = "ZAttrProvisioning." + enumName(ai);
/* 1065 */       if (defaultValue != null) {
/* 1066 */         defaultValue = javaType + "." + StringUtil.escapeJavaIdentifier(defaultValue);
/*      */       } else {
/* 1068 */         defaultValue = "null";
/*      */       }
/* 1070 */       javaBody = String.format("try { String v = getAttr(Provisioning.A_%s); return v == null ? %s : ZAttrProvisioning.%s.fromString(v); } catch(com.zimbra.common.service.ServiceException e) { return %s; }", new Object[] { name, defaultValue, enumName(ai), defaultValue });
/* 1071 */       javaDocReturns = String.format(", or %s if unset and/or has invalid value", new Object[] { defaultValue });
/* 1072 */       break;
/*      */     case TYPE_LONG: 
/* 1074 */       if (defaultValue == null) defaultValue = "-1";
/* 1075 */       javaType = "long";
/* 1076 */       javaBody = String.format("return getLongAttr(Provisioning.A_%s, %sL);", new Object[] { name, defaultValue });
/* 1077 */       javaDocReturns = String.format(", or %s if unset", new Object[] { defaultValue });
/* 1078 */       break;
/*      */     case TYPE_DURATION: 
/*      */       String defaultDurationStrValue;
/* 1081 */       if (defaultValue != null) {
/* 1082 */         String defaultDurationStrValue = " (" + defaultValue + ") ";
/* 1083 */         defaultValue = String.valueOf(DateUtil.getTimeInterval(defaultValue, -1L));
/*      */       } else {
/* 1085 */         defaultValue = "-1";
/* 1086 */         defaultDurationStrValue = "";
/*      */       }
/* 1088 */       javaBody = String.format("return getTimeInterval(Provisioning.A_%s, %sL);", new Object[] { name, defaultValue });
/* 1089 */       javaDocReturns = String.format(" in millseconds, or %s%s if unset", new Object[] { defaultValue, defaultDurationStrValue });
/* 1090 */       javaType = "long";
/* 1091 */       asStringDoc = true;
/* 1092 */       break;
/*      */     case TYPE_GENTIME: 
/* 1094 */       javaType = "Date";
/* 1095 */       javaBody = String.format("return getGeneralizedTimeAttr(Provisioning.A_%s, null);", new Object[] { name });
/* 1096 */       javaDocReturns = " as Date, null if unset or unable to parse";
/* 1097 */       asStringDoc = true;
/* 1098 */       break;
/*      */     case TYPE_EMAIL: case TYPE_EMAILP: case TYPE_CS_EMAILP: case TYPE_ID: default: 
/* 1100 */       if (ai.getCardinality() != AttributeCardinality.multi) {
/* 1101 */         if (defaultValue != null) {
/* 1102 */           defaultValue = "\"" + defaultValue.replace("\"", "\\\"") + "\"";
/*      */         } else {
/* 1104 */           defaultValue = "null";
/*      */         }
/* 1106 */         javaType = "String";
/* 1107 */         javaBody = String.format("return getAttr(Provisioning.A_%s, %s);", new Object[] { name, defaultValue });
/* 1108 */         javaDocReturns = String.format(", or %s if unset", new Object[] { defaultValue });
/*      */       } else {
/* 1110 */         javaType = "String[]";
/* 1111 */         String javaBody; if (defaultValue == null) {
/* 1112 */           javaBody = String.format("return getMultiAttr(Provisioning.A_%s);", new Object[] { name });
/*      */         } else {
/* 1114 */           javaBody = String.format("String[] value = getMultiAttr(Provisioning.A_%s); return value.length > 0 ? value : %s;", new Object[] { name, defaultValue });
/*      */         }
/* 1116 */         javaDocReturns = ", or empty array if unset";
/*      */       }
/*      */       break;
/*      */     }
/*      */     
/* 1121 */     result.append("\n    /**\n");
/* 1122 */     if (ai.getDescription() != null) {
/* 1123 */       result.append(FileGenUtil.wrapComments(StringUtil.escapeHtml(ai.getDescription()), 70, "     * "));
/* 1124 */       result.append("\n");
/*      */     }
/* 1126 */     if (ai.getType() == AttributeType.TYPE_ENUM) {
/* 1127 */       result.append("     *\n");
/* 1128 */       result.append(String.format("     * <p>Valid values: %s%n", new Object[] { ai.getEnumSet().toString() }));
/*      */     }
/* 1130 */     if (asStringDoc) {
/* 1131 */       result.append("     *\n");
/* 1132 */       result.append(String.format("     * <p>Use %sAsString to access value as a string.%n", new Object[] { methodName }));
/* 1133 */       result.append("     *\n");
/* 1134 */       result.append(String.format("     * @see #%sAsString()%n", new Object[] { methodName }));
/*      */     }
/* 1136 */     result.append("     *\n");
/* 1137 */     result.append(String.format("     * @return %s%s%n", new Object[] { name, javaDocReturns }));
/* 1138 */     if (ai.getSince() != null) {
/* 1139 */       result.append("     *\n");
/* 1140 */       result.append(String.format("     * @since ZCS %s%n", new Object[] { versionListAsString(ai.getSince()) }));
/*      */     }
/* 1142 */     result.append("     */\n");
/* 1143 */     result.append(String.format("    @ZAttr(id=%d)%n", new Object[] { Integer.valueOf(ai.getId()) }));
/* 1144 */     result.append(String.format("    public %s %s() {%n        %s%n    }%n", new Object[] { javaType, methodName, javaBody }));
/*      */   }
/*      */   
/*      */   private static String versionListAsString(List<Version> versions) {
/* 1148 */     if ((versions == null) || (versions.size() == 0))
/* 1149 */       return "";
/* 1150 */     if (versions.size() == 1) {
/* 1151 */       return ((Version)versions.iterator().next()).toString();
/*      */     }
/* 1153 */     StringBuilder sb = new StringBuilder();
/* 1154 */     for (Version version : versions) {
/* 1155 */       sb.append(version.toString()).append(",");
/*      */     }
/* 1157 */     sb.setLength(sb.length() - 1);
/* 1158 */     return sb.toString();
/*      */   }
/*      */   
/*      */   private static enum SetterType {
/* 1162 */     set,  add,  unset,  remove;
/*      */     
/*      */     private SetterType() {} }
/* 1165 */   private static void generateSetters(StringBuilder result, AttributeInfo ai, boolean asString, SetterType setterType) throws ServiceException { generateSetter(result, ai, asString, setterType, true);
/* 1166 */     generateSetter(result, ai, asString, setterType, false);
/*      */   }
/*      */   
/*      */ 
/*      */   private static void generateSetter(StringBuilder result, AttributeInfo ai, boolean asString, SetterType setterType, boolean noMap)
/*      */     throws ServiceException
/*      */   {
/* 1173 */     String name = ai.getName();
/*      */     
/* 1175 */     AttributeType type = asString ? AttributeType.TYPE_STRING : ai.getType();
/*      */     
/*      */ 
/* 1178 */     String methodName = ai.getName();
/* 1179 */     if (methodName.startsWith("zimbra")) methodName = methodName.substring(6);
/* 1180 */     methodName = setterType.name() + methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
/* 1181 */     if (asString) methodName = methodName + "AsString";
/*      */     String javaType;
/* 1183 */     String putParam; String javaType; String putParam; switch (type) {
/*      */     case TYPE_BOOLEAN: 
/* 1185 */       javaType = "boolean";
/* 1186 */       putParam = String.format("%s ? Provisioning.TRUE : Provisioning.FALSE", new Object[] { name });
/* 1187 */       break;
/*      */     case TYPE_BINARY: 
/*      */     case TYPE_CERTIFICATE: 
/* 1190 */       javaType = "byte[]";
/* 1191 */       putParam = String.format("%s==null ? \"\" : ByteUtil.encodeLDAPBase64(%s)", new Object[] { name, name });
/* 1192 */       break;
/*      */     case TYPE_INTEGER: 
/*      */     case TYPE_PORT: 
/* 1195 */       javaType = "int";
/* 1196 */       putParam = String.format("Integer.toString(%s)", new Object[] { name });
/* 1197 */       break;
/*      */     case TYPE_LONG: 
/* 1199 */       javaType = "long";
/* 1200 */       putParam = String.format("Long.toString(%s)", new Object[] { name });
/* 1201 */       break;
/*      */     case TYPE_GENTIME: 
/* 1203 */       javaType = "Date";
/* 1204 */       putParam = String.format("%s==null ? \"\" : DateUtil.toGeneralizedTime(%s)", new Object[] { name, name });
/* 1205 */       break;
/*      */     case TYPE_ENUM: 
/* 1207 */       javaType = "ZAttrProvisioning." + enumName(ai);
/* 1208 */       putParam = String.format("%s.toString()", new Object[] { name });
/* 1209 */       break;
/*      */     case TYPE_EMAIL: case TYPE_EMAILP: case TYPE_CS_EMAILP: case TYPE_ID: case TYPE_DURATION: default: 
/* 1211 */       if (ai.getCardinality() != AttributeCardinality.multi) {
/* 1212 */         javaType = "String";
/* 1213 */         putParam = String.format("%s", new Object[] { name });
/*      */       } else { String javaType;
/* 1215 */         if (setterType == SetterType.set) javaType = "String[]"; else
/* 1216 */           javaType = "String";
/* 1217 */         putParam = String.format("%s", new Object[] { name });
/*      */       }
/*      */       break;
/*      */     }
/*      */     
/* 1222 */     String mapType = "Map<String,Object>";
/*      */     
/* 1224 */     result.append("\n    /**\n");
/* 1225 */     if (ai.getDescription() != null) {
/* 1226 */       result.append(FileGenUtil.wrapComments(StringUtil.escapeHtml(ai.getDescription()), 70, "     * "));
/* 1227 */       result.append("\n");
/*      */     }
/* 1229 */     if (ai.getType() == AttributeType.TYPE_ENUM) {
/* 1230 */       result.append("     *\n");
/* 1231 */       result.append(String.format("     * <p>Valid values: %s%n", new Object[] { ai.getEnumSet().toString() }));
/*      */     }
/* 1233 */     result.append("     *\n");
/*      */     
/* 1235 */     String paramDoc = "";
/* 1236 */     String body = "";
/*      */     
/* 1238 */     switch (setterType) {
/*      */     case set: 
/* 1240 */       body = String.format("        attrs.put(Provisioning.A_%s, %s);%n", new Object[] { name, putParam });
/* 1241 */       paramDoc = String.format("     * @param %s new value%n", new Object[] { name });
/* 1242 */       break;
/*      */     case add: 
/* 1244 */       body = String.format("        StringUtil.addToMultiMap(attrs, \"+\" + Provisioning.A_%s, %s);%n", new Object[] { name, name });
/* 1245 */       paramDoc = String.format("     * @param %s new to add to existing values%n", new Object[] { name });
/* 1246 */       break;
/*      */     case remove: 
/* 1248 */       body = String.format("        StringUtil.addToMultiMap(attrs, \"-\" + Provisioning.A_%s, %s);%n", new Object[] { name, name });
/* 1249 */       paramDoc = String.format("     * @param %s existing value to remove%n", new Object[] { name });
/* 1250 */       break;
/*      */     case unset: 
/* 1252 */       body = String.format("        attrs.put(Provisioning.A_%s, \"\");%n", new Object[] { name });
/* 1253 */       paramDoc = null;
/*      */     }
/*      */     
/*      */     
/* 1257 */     if (paramDoc != null) result.append(paramDoc);
/* 1258 */     if (!noMap) {
/* 1259 */       result.append(String.format("     * @param attrs existing map to populate, or null to create a new map%n", new Object[0]));
/* 1260 */       result.append("     * @return populated map to pass into Provisioning.modifyAttrs\n");
/*      */     } else {
/* 1262 */       result.append("     * @throws com.zimbra.common.service.ServiceException if error during update\n");
/*      */     }
/* 1264 */     if (ai.getSince() != null) {
/* 1265 */       result.append("     *\n");
/* 1266 */       result.append(String.format("     * @since ZCS %s%n", new Object[] { versionListAsString(ai.getSince()) }));
/*      */     }
/* 1268 */     result.append("     */\n");
/* 1269 */     result.append(String.format("    @ZAttr(id=%d)%n", new Object[] { Integer.valueOf(ai.getId()) }));
/* 1270 */     if (noMap) {
/* 1271 */       if (setterType != SetterType.unset) {
/* 1272 */         result.append(String.format("    public void %s(%s %s) throws com.zimbra.common.service.ServiceException {%n", new Object[] { methodName, javaType, name }));
/*      */       } else
/* 1274 */         result.append(String.format("    public void %s() throws com.zimbra.common.service.ServiceException {%n", new Object[] { methodName }));
/* 1275 */       result.append(String.format("        HashMap<String,Object> attrs = new HashMap<String,Object>();%n", new Object[0]));
/* 1276 */       result.append(body);
/* 1277 */       result.append(String.format("        getProvisioning().modifyAttrs(this, attrs);%n", new Object[0]));
/*      */     } else {
/* 1279 */       if (setterType != SetterType.unset) {
/* 1280 */         result.append(String.format("    public %s %s(%s %s, %s attrs) {%n", new Object[] { mapType, methodName, javaType, name, mapType }));
/*      */       } else
/* 1282 */         result.append(String.format("    public %s %s(%s attrs) {%n", new Object[] { mapType, methodName, mapType }));
/* 1283 */       result.append(String.format("        if (attrs == null) attrs = new HashMap<String,Object>();%n", new Object[0]));
/* 1284 */       result.append(body);
/* 1285 */       result.append(String.format("        return attrs;%n", new Object[0]));
/*      */     }
/*      */     
/* 1288 */     result.append(String.format("    }%n", new Object[0]));
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   private void generateProvisioningConstants(String javaFile)
/*      */     throws ServiceException, IOException
/*      */   {
/* 1299 */     List<String> list = new ArrayList(getAttrs().keySet());
/* 1300 */     Collections.sort(list);
/*      */     
/* 1302 */     StringBuilder result = new StringBuilder();
/*      */     
/* 1304 */     for (String a : list) {
/* 1305 */       AttributeInfo ai = (AttributeInfo)getAttrs().get(a.toLowerCase());
/* 1306 */       if ((ai != null) && (ai.getType() == AttributeType.TYPE_ENUM))
/*      */       {
/* 1308 */         generateEnum(result, ai);
/*      */       }
/*      */     }
/* 1311 */     for (String a : list) {
/* 1312 */       AttributeInfo ai = (AttributeInfo)getAttrs().get(a.toLowerCase());
/* 1313 */       if (ai != null)
/*      */       {
/*      */ 
/* 1316 */         result.append("\n    /**\n");
/* 1317 */         if (ai.getDescription() != null) {
/* 1318 */           result.append(FileGenUtil.wrapComments(StringUtil.escapeHtml(ai.getDescription()), 70, "     * "));
/* 1319 */           result.append("\n");
/*      */         }
/* 1321 */         if (ai.getSince() != null) {
/* 1322 */           result.append("     *\n");
/* 1323 */           result.append(String.format("     * @since ZCS %s%n", new Object[] { versionListAsString(ai.getSince()) }));
/*      */         }
/* 1325 */         result.append("     */\n");
/* 1326 */         result.append(String.format("    @ZAttr(id=%d)%n", new Object[] { Integer.valueOf(ai.getId()) }));
/*      */         
/* 1328 */         result.append(String.format("    public static final String A_%s = \"%s\";%n", new Object[] { ai.getName(), ai.getName() }));
/*      */       }
/*      */     }
/* 1331 */     FileGenUtil.replaceJavaFile(javaFile, result.toString());
/*      */   }
/*      */   
/*      */   private static void usage(String errmsg)
/*      */   {
/* 1336 */     if (errmsg != null) {
/* 1337 */       logger.error(errmsg);
/*      */     }
/* 1339 */     HelpFormatter formatter = new HelpFormatter();
/* 1340 */     formatter.printHelp("AttributeManagerUtil [options] where [options] are one of:", options);
/* 1341 */     System.exit(errmsg == null ? 0 : 1);
/*      */   }
/*      */   
/*      */   private static CommandLine parseArgs(String[] args) {
/* 1345 */     StringBuffer gotCL = new StringBuffer("cmdline: ");
/* 1346 */     for (int i = 0; i < args.length; i++) {
/* 1347 */       gotCL.append("'").append(args[i]).append("' ");
/*      */     }
/*      */     
/*      */ 
/* 1351 */     CommandLineParser parser = new GnuParser();
/* 1352 */     CommandLine cl = null;
/*      */     try {
/* 1354 */       cl = parser.parse(options, args);
/*      */     } catch (ParseException pe) {
/* 1356 */       usage(pe.getMessage());
/*      */     }
/* 1358 */     if (cl.hasOption('h')) {
/* 1359 */       usage(null);
/*      */     }
/* 1361 */     return cl;
/*      */   }
/*      */   
/*      */   public static void main(String[] args) throws IOException, ServiceException {
/* 1365 */     CliUtil.toolSetup();
/* 1366 */     CommandLine cl = parseArgs(args);
/*      */     
/* 1368 */     if (!cl.hasOption('a')) usage("no action specified");
/* 1369 */     String actionStr = cl.getOptionValue('a');
/* 1370 */     Action action = null;
/*      */     try {
/* 1372 */       action = Action.valueOf(actionStr);
/*      */     } catch (IllegalArgumentException iae) {
/* 1374 */       usage("unknown action: " + actionStr);
/*      */     }
/*      */     
/* 1377 */     AttributeManager am = null;
/* 1378 */     if ((action != Action.dump) && (action != Action.listAttrs)) {
/* 1379 */       if (!cl.hasOption('i')) usage("no input attribute xml files specified");
/* 1380 */       am = new AttributeManager(cl.getOptionValue('i'));
/* 1381 */       if (am.hasErrors()) {
/* 1382 */         ZimbraLog.misc.warn(am.getErrors());
/* 1383 */         System.exit(1);
/*      */       }
/*      */     }
/*      */     
/* 1387 */     OutputStream os = System.out;
/* 1388 */     if (cl.hasOption('o')) {
/* 1389 */       os = new FileOutputStream(cl.getOptionValue('o'));
/*      */     }
/* 1391 */     PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(os, "utf8")));
/*      */     
/* 1393 */     AttributeManagerUtil amu = new AttributeManagerUtil(am);
/*      */     
/* 1395 */     switch (action) {
/*      */     case dump: 
/* 1397 */       LdapProv.getInst().dumpLdapSchema(pw);
/* 1398 */       break;
/*      */     case generateDefaultCOSLdif: 
/* 1400 */       amu.generateDefaultCOSLdif(pw);
/* 1401 */       break;
/*      */     case generateDefaultExternalCOSLdif: 
/* 1403 */       amu.generateDefaultExternalCOSLdif(pw);
/* 1404 */       break;
/*      */     case generateGetters: 
/* 1406 */       amu.generateGetters(cl.getOptionValue('c'), cl.getOptionValue('r'));
/* 1407 */       break;
/*      */     case generateGlobalConfigLdif: 
/* 1409 */       amu.generateGlobalConfigLdif(pw);
/* 1410 */       break;
/*      */     case generateLdapSchema: 
/* 1412 */       if (!cl.hasOption('t')) {
/* 1413 */         usage("no schema template specified");
/*      */       }
/* 1415 */       amu.generateLdapSchema(pw, cl.getOptionValue('t'));
/* 1416 */       break;
/*      */     case generateMessageProperties: 
/* 1418 */       amu.generateMessageProperties(cl.getOptionValue('r'));
/* 1419 */       break;
/*      */     case generateProvisioning: 
/* 1421 */       amu.generateProvisioningConstants(cl.getOptionValue('r'));
/* 1422 */       break;
/*      */     case generateSchemaLdif: 
/* 1424 */       amu.generateSchemaLdif(pw);
/* 1425 */       break;
/*      */     case listAttrs: 
/* 1427 */       amu.listAttrs(pw, cl.getOptionValues('c'), cl.getOptionValues('n'), cl.getOptionValues('f'));
/*      */     }
/*      */     
/*      */     
/* 1431 */     pw.close();
/*      */   }
/*      */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/AttributeManagerUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */